<?php

//namespace Login\Model;

class RegisterModel{
	
	private $m_connection = NULL;	
	
	public function __construct(DBConnection $connection)
	{
		$this->m_connection = $connection;
	}
	
	private function IsUserExist($username)
	{
		$sql = "SELECT * FROM user WHERE Username=?";
				
		$stmt = $this->m_connection->Prepare($sql);
			
		$stmt->bind_param("s", $username);
						
		$stmt->execute();
			
		$result = $stmt->get_result();
			
		$row_count = $result->num_rows;
			
		if($row_count == 1)
		{
			$bool = true;
		}
		else{
			$bool = false;
		}
		$result->close();
		return $bool;
	}
	
	public function Register($username, $password)
	{
		if($this->IsUserExist($username)){
			return false;
		}
		$sql = "INSERT INTO `lab3`.`user` (`Username`, `Password`) VALUES (?, ?);";
		$stmt = $this->m_connection->Prepare($sql);
		$stmt->bind_param("ss", $username, $password);
		$stmt->execute();
		return true;
	}
	
	public function DeleteUser($username){
		
		if($this->IsUserExist($username)){
			$sql = "DELETE FROM `lab3`.`user` WHERE Username = ?;";
			$stmt = $this->m_connection->Prepare($sql);
			$stmt->bind_param("s", $username);
			$stmt->execute();
			return true;
		}
		return false;
	}
}